package com.kx.game.center.manager;

import com.alibaba.fastjson.JSONObject;
import com.kx.core.dawn.dbs.dao.DaoManager;
import com.kx.core.dawn.dbs.entity.GameServerEntity;
import com.kx.game.center.common.ServerEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;

public class GameServerManager {

    private static Logger logger = LoggerFactory.getLogger(GameServerManager.class);

    public void update(GameServerEntity gameServerEntity, int loginNum,
                       String ip, String gamePort, String gatePort,
                       int createNum, String timeMillis, String connectIp, String connectPort) {
        gameServerEntity.setLoginNum(loginNum);
        gameServerEntity.setIp(ip);
        gameServerEntity.setGamePort(gamePort);
        gameServerEntity.setGatePort(gatePort);
        gameServerEntity.setConnectIp(connectIp);
        gameServerEntity.setConnectPort(connectPort);
        gameServerEntity.setCreateNum(createNum);
        gameServerEntity.setTime(timeMillis);
        gameServerEntity.setType(ServerEnum.GAMER.getIndex() + "");
        DaoManager.gameServerDao.update(gameServerEntity);
    }

    public void insert(String serverId, int loginNum, int createNum,
                       String ip, String gamePort, String gatePort, String connectIp, String connectPort,
                       String state, String type, String timeMillis) {
        GameServerEntity gameServerEntity = new GameServerEntity();
        gameServerEntity.setServerId(serverId);
        gameServerEntity.setLoginNum(loginNum);
        gameServerEntity.setIp(ip);
        gameServerEntity.setGamePort(gamePort);
        gameServerEntity.setGatePort(gatePort);
        gameServerEntity.setConnectIp(connectIp);
        gameServerEntity.setConnectPort(connectPort);
        gameServerEntity.setCreateNum(createNum);
        gameServerEntity.setTime(timeMillis + "");
        gameServerEntity.setState(state);
        gameServerEntity.setType(type);
        DaoManager.gameServerDao.insert(gameServerEntity);
    }

    public void printLog(HttpServletRequest request, Logger logger, String param) throws Exception {
        logger.info("Ip地址:{} port:{}", request.getRemoteHost(), request.getRemotePort());
        logger.info("url地址:{}", request.getRequestURL());
        String queryString = request.getQueryString();
        if (queryString != null)
            queryString = URLDecoder.decode(queryString, "utf-8");
        logger.info("{} 数据:{}", param, queryString);
    }

    public void printLog(HttpServletRequest request, JSONObject jsonObject, Logger logger, String param) throws Exception {
        logger.info("Ip地址:{} port:{}", request.getRemoteHost(), request.getRemotePort());
        logger.info("url地址:{}", request.getRequestURL());
        String queryString = jsonObject.toString();
        if (queryString != null)
            queryString = URLDecoder.decode(queryString, "utf-8");
        logger.info("{} 数据:{}", param, queryString);
    }
}
